<?php
class RequirementsController extends AppController {

	var $name = 'Requirements';
	//	var $helpers = array('Html', 'Form','Javascript','Jmycake');
	var $uses = array('Requirement', 'ProcedureRequirement', 'Procedure');
	var $helpers = array('Ajax', 'Text');
	var $components = array('RequestHandler', 'Auth');


	function index() {
		$this->Requirement->recursive = 0;
		$this->set('requirements', $this->paginate(null, array('r_project' => $this->Session->read('id_projet'))));
	}

	function view($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Requirement', true), array('action'=>'index'));
		}
		$this->set('requirement', $this->Requirement->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Requirement->create();
			//			print_r($this->Session->read()) ;
			$this->Requirement->saveField('r_project', $this->Session->read('id_projet'));
			if ($this->Requirement->save($this->data)) {
				$this->flash(__('Requirement saved.', true), array('action'=>'index'));
				$this->_stop();
				$this->redirect(array('action'=>'index'));
			} else {
				$this->flash(__('The Requirement hasn\'t been saved. Retry', true), array('action'=>'index'));
			}
		}
		$projects = $this->Requirement->Project->find('list');
		$categories = $this->Requirement->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name')));
		$applicabilities = $this->Requirement->Applicability->find('list', array ('fields' => array('Applicability.app_index', 'Applicability.app_description')));
		$conformances = $this->Requirement->Conformance->find('list', array ('fields' => array('Conformance.conf_index', 'Conformance.conf_description')));
		$this->set(compact('projects', 'categories', 'applicabilities', 'conformances'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->flash(__('Invalid Requirement', true), array('action'=>'index'));
			$this->_stop();
		}
		if (!empty($this->data)) {
			$this->data['Requirement']['r_index'] = $id ;
			if ($this->Requirement->save($this->data)) {
				$this->flash(__('The Requirement has been saved.', true), array('action'=>'index'));
				$this->_stop();
				$this->redirect(array('action'=>'index'));
			} else {
//				$this->Requirement->del($id) ;
//				if (! $this->Requirement->save($this->data)){
					$this->flash(__('The Requirement hasn\'t been saved. Retry', true), array('action'=>'index'));
//				}
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Requirement->read(null, $id);
		}
		$status = $this->Requirement->find('list', array ('fields' => 'Requirement.r_status'));
		$projects = $this->Requirement->Project->find('list', array ('fields' => array('Project.proj_index', 'Project.proj_name')));
		$categories = $this->Requirement->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name')));
		$applicabilities = $this->Requirement->Applicability->find('list', array ('fields' => array('Applicability.app_index', 'Applicability.app_description')));
		$conformances = $this->Requirement->Conformance->find('list', array ('fields' => array('Conformance.conf_index', 'Conformance.conf_description')));
		$this->set(compact('projects', 'categories', 'applicabilities', 'conformances', 'status'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Requirement', true), array('action'=>'index'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Requirement->del($id)) {
			$this->flash(__('Requirement deleted', true), array('action'=>'index'));
			$this->redirect(array('action'=>'index'));
		}
	}

	function link(){
		//		Récupération de la catégorie :
		$idCat = utf8_decode($this->data['Requirement']['r_category']) ;
		//		S'il n'y a pas de catégorie choisie (1er chargement), mise à défaut :
		if ( $idCat == null) {
			$cat_init = $this->Requirement->Category->find('first', array ('fields' => array('Category.cat_index')));
			$idCat = $cat_init['Category']['cat_index'];
		}

		$procedure = null;
		//		Si une procédure a été sélectionnée, on récupère l'id de la procédure :
		if ( isset($this->data['Procedure']['proc_reference']) && $this->data['Procedure']['proc_reference']!= null ){
			$procedure = $this->Procedure->find('first', array ('conditions' => array('proc_reference' => $this->data['Procedure']['proc_reference'], 'proc_project' => $this->Session->read('id_projet'))));

			$this->data['Procedure']['proc_index'] = $procedure['Procedure']['proc_index'] ;
			$this->data['Procedure']['proc_reference'] = $procedure['Procedure']['proc_reference'] ;
			$this->set('proc_reference', ($procedure['Procedure']['proc_reference']));
			$this->set('proc_title', ($procedure['Procedure']['proc_title']));
			$this->set('proc_ptype', ($procedure['Procedure']['proc_ptype']));
			$this->set('proc_phase', ($procedure['Procedure']['proc_phase']));
			$this->set('proc_part', ($procedure['Procedure']['proc_part']));
			$this->set('proc_version', ($procedure['Procedure']['proc_version']));
			$this->set('walle', $procedure);
				
		}
		
		$requirementsDispos = null ;
		$requirementsAssocies = null ;
		if ($procedure != null){
			$requirementsDispos = 
				$this->Requirement->query('Select R1.r_index, R1.r_reference FROM requirements R1 WHERE R1.r_project = '.$this->Session->read('id_projet').' AND R1.r_index NOT IN ( SELECT PR.pra_requirement FROM procedure_requirements PR WHERE PR.pra_proc = '.$procedure['Procedure']['proc_index']." )");
			$requirementsAssocies = 
				$this->Requirement->query('Select RA.r_index, RA.r_reference FROM requirements RA WHERE RA.r_project = '.$this->Session->read('id_projet').' AND RA.r_index IN ( SELECT PR.pra_requirement FROM procedure_requirements PR WHERE PR.pra_proc = '.$procedure['Procedure']['proc_index']." )");
		}

//		$this->set('walle', $requirementsAssocies);
		$this->set('idCat', $idCat);
		$this->set('requirementsDispos', $requirementsDispos);
		$this->set('requirementsAssocies', $requirementsAssocies);
		
		$this->set('categories', $this->Requirement->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name'))));
	}


	function autocomplete($idCat)
	{
		$recherche = utf8_decode($this->data['Procedure']['proc_reference']);

		if ( $idCat == null ){
			$this->flash(__('/!\ Catégorie non valide', true), array('controller' => 'requirements', 'action'=>'index'));
		}

		$references = $this->Procedure->find(
			'all',
		array(
				'fields' => 'DISTINCT proc_reference',
				'conditions' => "proc_reference LIKE '%$recherche%' AND proc_cat = ".$idCat,
				'order' => 'proc_reference',
				'limit' => 10
		)
		);
		$this->set(compact('references', 'recherche'));
	}



}
?>